package com.bupt.secret.enums;

/**
 * 错误代码枚举
 */
public enum BuptErrorCodeEnum {
    SDR_OK(0x0, "操作成功"),
    SDR_UNKNOWNERR(0x01000000 + 0x00000001, "未知错误"), // 16777217
    SDR_NOTSUPPORT(0x01000000 + 0x00000002, "不支持的接口调用"), // 16777218
    SDR_COMMFAIL(0x01000000 + 0x00000003, "与设备通信失败"), // 16777219
    SDR_HARDFAIL(0x01000000 + 0x00000004, "运算模块无响应"), // 16777220
    SDR_OPENDEVICE(0x01000000 + 0x00000005, "打开设备失败"), // 16777221
    SDR_OPENSESSION(0x01000000 + 0x00000006, "创建会话失败"), // 16777222
    SDR_PARDENY(0x01000000 + 0x00000007, "无私钥使用权限"), // 16777223
    SDR_KEYNOTEXIST(0x01000000 + 0x00000008, "不存在的密钥调用"), // 16777224
    SDR_ALGNOTSUPPORT(0x01000000 + 0x00000009, "不支持的算法调用"), // 16777225
    SDR_ALGMODNOTSUPPORT(0x01000000 + 0x0000000A, "不支持的算法模式调用"), // 16777226
    SDR_PKOPERR(0x01000000 + 0x0000000B, "公钥运算失败"), // 16777227
    SDR_SKOPERR(0x01000000 + 0x0000000C, "私钥运算失败"), // 16777228
    SDR_SIGNERR(0x01000000 + 0x0000000D, "签名运算失败"), // 16777229
    SDR_VERIFYERR(0x01000000 + 0x0000000E, "验证签名失败"), // 16777230
    SDR_SYNOPERR(0x01000000 + 0x0000000F, "对称算法运算失败"), // 16777231
    SDR_STEPERR(0x01000000 + 0x00000010, "多步运算步骤失败"), // 16777232
    SDR_FILESIZEERR(0x01000000 + 0x00000011, "文件长度超出限制"), // 16777233
    SDR_FILENOTEXIST(0x01000000 + 0x00000012, "指定文件不存在"), // 16777234
    SDR_FILEOFSERR(0x01000000 + 0x00000013, "文件起始位置错误"), // 16777235
    SDR_KEYTYPEERR(0x01000000 + 0x00000014, "密钥类型错误"), // 16777236
    SDR_KEYERR(0x01000000 + 0x00000015, "密钥错误"), // 16777237
    SDR_ENCDATAERR(0x01000000 + 0x00000016, "ECC加密数据错误"), // 16777238
    SDR_RANDERR(0x01000000 + 0x00000017, "随机数产生失败"), // 16777239
    SDR_PRKRERR(0x01000000 + 0x00000018, "私钥使用权限获取失败"), // 16777240
    SDR_MACERR(0x01000000 + 0x00000019, "MAC运算失败"), // 16777241
    SDR_FILEEXISTS(0x01000000 + 0x0000001A, "指定文件已经存在"), // 16777242
    SDR_FILEWERR(0x01000000 + 0x0000001B, "文件写入失败"), // 16777243
    SDR_NOBUFFER(0x01000000 + 0x0000001C, "存储空间不足"), // 16777244
    SDR_INARGERR(0x01000000 + 0x0000001D, "输入参数错误"), // 16777245
    SDR_OUTARGERR(0x01000000 + 0x0000001E, "输出参数错误"),// 16777246


    // 自定义异常
    BUPT_SESSION_IS_NULL(13100, "回话句柄参数错误"),
    BUPT_HKEYHANDLE_IS_NULL(13101, "密钥句柄参数错误"),
    BUPT_PUI_KEY_LENGTH_ZERO(13111, "密钥长度参数错误"),
    BUPT_RSAREFPUBLICKEY_NULL(13112, "公钥参数错误"),
    BUPT_PUCKEY_ERROR(13113, "会话密钥参数错误"),

    ;

    private final int code;
    private final String description;

    BuptErrorCodeEnum(int code, String description) {
        this.code = code;
        this.description = description;
    }

    public int getCode() {
        return code;
    }

    public String getDescription() {
        return description;
    }

    public static BuptErrorCodeEnum fromCode(int code) {
        for (BuptErrorCodeEnum errorCode : values()) {
            if (errorCode.getCode() == code) {
                return errorCode;
            }
        }
        return null;
    }

}